package com.zr.bookcorner.broadcast;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import androidx.annotation.RequiresPermission;

import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.utils.LogUtils;


public class NetworkReceiver extends BroadcastReceiver {
    @RequiresPermission(Manifest.permission.ACCESS_NETWORK_STATE)
    @Override
    public void onReceive(Context context, Intent intent) {
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            ConnectivityManager manager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            if (manager == null) return;
            NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
            if (activeNetwork != null) {
                if (activeNetwork.isConnected()) {
                    LogUtils.d("当前网络");
                    BaseConstant.hasNet = true;

                } else {
                    LogUtils.d("当前没有可用网络");
                    BaseConstant.hasNet = false;
                }
            }
        }
    }
}
